package com.xgame.server.dbtool;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.xgame.server.db.mapper.ShoprefreshMapper;
import com.xgame.server.db.model.Shoprefresh;
import com.xgame.server.netty.proto.Game;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class ShoprefreshCsTool {
    @Resource
    private ShoprefreshMapper shoprefreshMapper;

    //插入玩家记录
    public Integer insertShopRefresh(Game.ShopRefreshCs data) throws Exception {

        Shoprefresh account = new Shoprefresh();
        account.setAccount(data.getAccount());
        account.setPlayerId(data.getPlayerID());

        String lst = "";
        List<Game.ShopOneCs> items = data.getShopOneLstList();
        int length = items.size();
        for (int i = 0; i < length; i++) {
            if (i == length - 1) {
                lst = lst + items.get(i).getId() + ";" + items.get(i).getCount() + ";" + items.get(i).getSale() + ";" + items.get(i).getSoldout();
            } else {
                lst = lst + items.get(i).getId() + ";" + items.get(i).getCount() + ";" + items.get(i).getSale() + ";" + items.get(i).getSoldout() + "^";
            }
        }
        account.setShopOne(lst);

        lst = "";
        items = data.getShopTwoLstList();
        length = items.size();
        for (int i = 0; i < length; i++) {
            if (i == length - 1) {
                lst = lst + items.get(i).getId() + ";" + items.get(i).getCount() + ";" + items.get(i).getSale() + ";" + items.get(i).getSoldout();
            } else {
                lst = lst + items.get(i).getId() + ";" + items.get(i).getCount() + ";" + items.get(i).getSale() + ";" + items.get(i).getSoldout() + "^";
            }
        }
        account.setShopTwo(lst);

        account.setOneRefresh(data.getOneRefresh());
        account.setTwoRefresh(data.getTwoRefresh());

        lst = "";
        List<Game.ChipBattleOneCs> items2 = data.getChipBatLstList();
        length = items2.size();
        for (int i = 0; i < length; i++) {
            if (i == length - 1) {
                lst = lst + items2.get(i).getId() + ";" + items2.get(i).getChance() + ";" + items2.get(i).getRefresh() + ";" + items2.get(i).getOver();
            } else {
                lst = lst + items2.get(i).getId() + ";" + items2.get(i).getChance() + ";" + items2.get(i).getRefresh() + ";" + items2.get(i).getOver() + "^";
            }
        }
        account.setChipBatLst(lst);
        account.setLastDieTime(data.getLastDieTime());
        account.setSpawnInterval(new Long(data.getSpawnInterval()));

        lst = "";
        Game.HeroIslandOneCs items3 = data.getHeroIslandLst();
        lst = lst + items3.getResetTime() + ";" + items3.getTopLevel() + ";" + items3.getCurrentLevel() + ";" + items3.getGetBoxId();
        account.setHeroIslandLst(lst);

        return shoprefreshMapper.insert(account);
    }

    //更新玩家记录
    public Integer updateShopRefresh(Game.ShopRefreshCs data) throws Exception {
        EntityWrapper<Shoprefresh> userWrapper = new EntityWrapper<>();
        userWrapper.eq("account", data.getAccount());

        Shoprefresh account = new Shoprefresh();
        account.setAccount(data.getAccount());
        account.setPlayerId(data.getPlayerID());

        String lst = "";
        List<Game.ShopOneCs> items = data.getShopOneLstList();
        int length = items.size();
        for (int i = 0; i < length; i++) {
            if (i == length - 1) {
                lst = lst + items.get(i).getId() + ";" + items.get(i).getCount() + ";" + items.get(i).getSale() + ";" + items.get(i).getSoldout();
            } else {
                lst = lst + items.get(i).getId() + ";" + items.get(i).getCount() + ";" + items.get(i).getSale() + ";" + items.get(i).getSoldout() + "^";
            }
        }
        account.setShopOne(lst);

        lst = "";
        items = data.getShopTwoLstList();
        length = items.size();
        for (int i = 0; i < length; i++) {
            if (i == length - 1) {
                lst = lst + items.get(i).getId() + ";" + items.get(i).getCount() + ";" + items.get(i).getSale() + ";" + items.get(i).getSoldout();
            } else {
                lst = lst + items.get(i).getId() + ";" + items.get(i).getCount() + ";" + items.get(i).getSale() + ";" + items.get(i).getSoldout() + "^";
            }
        }
        account.setShopTwo(lst);

        account.setOneRefresh(data.getOneRefresh());
        account.setTwoRefresh(data.getTwoRefresh());

        lst = "";
        List<Game.ChipBattleOneCs> items2 = data.getChipBatLstList();
        length = items2.size();
        for (int i = 0; i < length; i++) {
            if (i == length - 1) {
                lst = lst + items2.get(i).getId() + ";" + items2.get(i).getChance() + ";" + items2.get(i).getRefresh() + ";" + items2.get(i).getOver();
            } else {
                lst = lst + items2.get(i).getId() + ";" + items2.get(i).getChance() + ";" + items2.get(i).getRefresh() + ";" + items2.get(i).getOver() + "^";
            }
        }
        account.setChipBatLst(lst);
        account.setLastDieTime(data.getLastDieTime());
        account.setSpawnInterval(new Long(data.getSpawnInterval()));

        lst = "";
        Game.HeroIslandOneCs items3 = data.getHeroIslandLst();
        lst = lst + items3.getResetTime() + ";" + items3.getTopLevel() + ";" + items3.getCurrentLevel() + ";" + items3.getGetBoxId();
        account.setHeroIslandLst(lst);

        return shoprefreshMapper.update(account, userWrapper);
    }

    //删除记录
    public Integer deleteShopRefresh(Game.ShopRefreshCs data) throws Exception {
        EntityWrapper<Shoprefresh> userWrapper = new EntityWrapper<>();
        userWrapper.eq("account", data.getAccount());

        return shoprefreshMapper.delete(userWrapper);
    }

    //查询记录
    public List<Shoprefresh> selectShopRefresh(String account) throws Exception {
        EntityWrapper<Shoprefresh> userWrapper = new EntityWrapper<>();
        userWrapper.eq("account", account);

        return shoprefreshMapper.selectList(userWrapper);
    }
}
